Functions and RPG

⏱️ DO IT NOW

Create a OneNote Page and copy this into the title:

Lesson 5-6 - Spring 2 - Functions and RPG



Click this link: VSCODEEDU.COM


  • Sign In with your Microsoft Account
  • Create a New project and call it Y8 - SP2.5-6 - Functions and RPG
  • Rename main.py as DIN.py
  • Share a link to your project on the OneNote page
  • In VSCODE, write code that will result in the output below⬇️⬇️

Name the capital of Iceland: Rekjavik

Correct, the capital of Iceland is Rekjavik

🎯 Learning Objectives


  • LO1:We will be able to use selection, input, variables and outputs within our programming
  • LO2:We will be able to explain how functions enhance the nature of a program
  • LO3:We will be able to create a program that incorporates the use of functions

📖 Everybody Reads

  • Functions are reusable blocks of code (similar to the concept of a template)
  • Because functions are reusable we don't need to write the same code repeatedly
  • Each function can be called into action at any point in a program
  • Each function can be called into action more than once in a program

Coding Functions (1)

  • Create a new file in your VSCODE project
  • Call it Activity 1.py
  • Add the code below to it ⬇️⬇️

  • #------------------- Functions-------------------------
    def kenya():
        question = input("What is the capital of Kenya? ")
        if question.lower() == "nairobi":
            print("Correct")
        else:
            print("Incorrect")
    
  • Try to run the code
  • Does anything happen?

Coding Functions (2)

  • Now Add the code below to it ⬇️⬇️

  • #------------------- MAIN PROGRAM-------------------------
    
    print("The program will now ask you a question")
    
    kenya()
    
    
  • Try to run the code again
  • Does anything happen?

Coding Functions (3)

⌨️ Independent Challenge

  • Here is a new function definition
  • See if you can work out where to put it

def adding():
    num1 = 8
    num2 = 9
    answer = num1 + num2
    print(f"{num1} + {num2} = {answer}")  
   

  • Here is a call for the function
  • See if you can work out where to put it

adding()
   
Only click this if you get really stuck!
#------------------- Functions-------------------------

def kenya():
    question = input("What is the capital of Kenya? ")
    if question.lower() == "nairobi":
        print("Correct")
    else:
        print("Incorrect")



def adding():
    num1 = 8
    num2 = 9
    answer = num1 + num2
    print(f"{num1} + {num2} = {answer}")


#------------------- MAIN PROGRAM-------------------------

print("The program will now ask you a question")
kenya()

print("This will add two numbers together")
adding()

Coding Functions (4)

📝 MWB Reflection Task

Open the mini-whiteboard app in a new tab

Your teacher will start with these leading questions:

  • Where do function definitions go?
  • What must we do in order to run a function?
  • style="user-select:all; cursor:pointer;" >How many times can you use a functionin your code?

RPG CODE

  • Create a new file in your VSCODE project
  • Call it RPG GAME.py
  • You will now develop more functions to create new levels in this game!
  • Notice how one function can be called within another
  • Firstly, run it to see how it works!

# Hallway
################################################

def hall():
    print("")
    print("You are in the hallway")
    print("Where would you like to go?")
    print("(1) Room 1")
    print("(2) Room 2")

    choice = input("Enter your choice: ")

    if choice == "1":
        room1()
    elif choice == "2":
        room2()
    else:
        print("Invalid choice - back to the hallway")
        hall()


# CODE FOR ROOM 1
#################################################
def room1():
    print("")
    print("You are in room 1")

    # ADD CODE FOR ROOM 1 HERE
    
    print("Type h to go back to the hallway")
    choice = input("Enter your choice")
    if choice.lower() == "h":
        hall()
    else:
        room1()



# CODE FOR ROOM 2
################################################
def room2():
    print("")
    print("You are in room 2")

    # ADD CODE FOR ROOM 2 HERE
    
    print("Type h to go back to the hallway")
    choice = input("Enter your choice")
    if choice.lower() == "h":
        hall()
    else:
        room2()



##########################################################
#-------------------- Main Program ----------------------
# This runs the functions
print("Welcome to the haunted mansion")
print("You have just walked through the main entrance")

hall()


Plenary Activity

📝 MWB Reflection Task

Open the mini-whiteboard app in a new tab

Your teacher will start with these leading questions:

  • What term describes using a function in the main program?
  • How do you move from one level to the next?